package com.huicoo.forestmanager.util;

import android.content.Context;
import android.graphics.Color;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.network.bean.patrol.PatrollingTasksinif;
import com.huicoo.forestmanager.network.bean.patrol.RecorddetailsBean;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.overlay.MarkerOverlay;
import com.tianditu.android.maps.overlay.PolygonOverlay;
import com.tianditu.android.maps.overlay.PolylineOverlay;
import com.tianditu.android.maps.renderoption.LineOption;
import com.tianditu.android.maps.renderoption.PlaneOption;
import com.tianditu.maps.Map.Project;

import java.util.ArrayList;
import java.util.List;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/8		    覆盖物工具类
 */
public class MulchUtil {
    /**
     * 添加林地覆盖物
     */
    public static void addWoodlandMulch(Context context, MapView map_view, int location, List<PatrollingTasksinif> patrollingTasks, MarkerOverlay.OnMarkerClickListener listener) {
        ArrayList<MarkerOverlay> objects = new ArrayList<>();
        //防止空指针
        if (patrollingTasks != null && patrollingTasks.size() > 0) {
            for (int i = 0; i < patrollingTasks.size(); i++) {
                if (patrollingTasks.get(i) != null && patrollingTasks.get(i).getForestArea() != null && patrollingTasks.get(i).getForestArea().getCenter() != null) {
                    GeoPoint geoPoint = new GeoPoint(
                            (int) (patrollingTasks.get(i).getForestArea().getCenter().getLatitude() * 1E6),
                            (int) (patrollingTasks.get(i).getForestArea().getCenter().getLongitude() * 1E6));
                    MarkerOverlay markerOverlay = new MarkerOverlay();
                    markerOverlay.setPosition(geoPoint);
//                if (i==location){
//                    markerOverlay.setIcon(context.getResources().getDrawable(R.mipmap.map_marker_hot));
//                }else {
                    markerOverlay.setIcon(context.getResources().getDrawable(R.mipmap.map_marker));
//                }
                    markerOverlay.setTitle("" + i);
                    markerOverlay.setClickListener(listener);
                    objects.add(markerOverlay);
                    map_view.addOverlay(objects.get(i));
                }
            }


        }
    }


    /**
     * 中心定位
     * （最大经度+最小经度）/2  =  中心点经度
     * （最大纬度+最小纬度）/2  =  中心点纬度
     */
    public static void centerPositioning(MapView map_view, List<PatrollingTasksinif> patrollingTasks) {
        ArrayList<Integer> latitude = new ArrayList<>();
        ArrayList<Integer> longitude = new ArrayList<>();

        for (int i = 0; i < patrollingTasks.size(); i++) {
            PatrollingTasksinif patrollingTasksinif = patrollingTasks.get(i);
            PatrollingTasksinif.ForestAreainif.Centerinif center = patrollingTasksinif.getForestArea().getCenter();
            if (center != null) {
                latitude.add((int) (center.getLatitude() * 1E6));
                longitude.add((int) (center.getLongitude() * 1E6));
            }
        }

        if (latitude.isEmpty() || longitude.isEmpty()) {
            return;
        }

        int biggestLatitude = 0;
        //获取最大纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer > biggestLatitude) {
                biggestLatitude = integer;
            }
        }


        int minimumLatitude = latitude.get(0);
        //获取最小纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer < minimumLatitude) {
                minimumLatitude = integer;
            }
        }


        int biggestLongitude = 0;
        //获取最大经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer > biggestLongitude) {
                biggestLongitude = integer;
            }
        }


        int minimumLongitude = longitude.get(0);
        //获取最小经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer < minimumLongitude) {
                minimumLongitude = integer;
            }
        }

        //通过经纬度缩放级别
        map_view.getController().setZoom(getZoom(biggestLongitude, minimumLongitude, biggestLatitude, minimumLatitude));

        GeoPoint point = new GeoPoint((biggestLatitude + biggestLatitude) / 2, (biggestLongitude + minimumLongitude) / 2);
        //动画移动到当前位置
        map_view.getController().animateTo(point);
    }


    //通过经纬度缩放级别
    public static int getZoom(int maxJ, int minJ, int maxW, int minW) {
        if (maxJ == minJ && maxW == minW) return 13;
        double diff = maxJ - minJ;
        if (diff < (maxW - minW) * 2.1) {
            diff = (maxW - minW) * 2.1;
        }
        diff = (10000 * diff) / 10000;


        ArrayList<Integer> zoomArr = new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
            zoomArr.add(i);
        }

        ArrayList<Double> diffArr = new ArrayList<>();
        diffArr.add(180.0);
        diffArr.add(90.0);
        diffArr.add(45.0);
        diffArr.add(11.0);
        diffArr.add(5.5);
        diffArr.add(2.75);
        diffArr.add(1.37);
        diffArr.add(0.68);
        diffArr.add(0.34);
        diffArr.add(0.17);
        diffArr.add(0.085);
        diffArr.add(0.043);
        diffArr.add(0.03);
        diffArr.add(0.02);
        diffArr.add(0.01);

        LogUtils.v("gogogo diff = " + (diff/1E6));

        for (int i = 0; i < diffArr.size(); i++) {
            if (((diff / 1E6) - diffArr.get(i)) >= 0) {
                LogUtils.v("gogogo diff get = " + zoomArr.get(i));
                return zoomArr.get(i);
            }
        }
        return 16;
    }


    /**
     * 添加图形覆盖物
     */
    public static void addCustomImgMulch(Context context, MapView map_view, GeoPoint geoPoint, int imgRes) {
        if (geoPoint == null) {
            return;
        }
        MarkerOverlay markerOverlay = new MarkerOverlay();
        markerOverlay.setPosition(geoPoint);
        markerOverlay.setIcon(context.getResources().getDrawable(imgRes));
        map_view.addOverlay(markerOverlay);
    }

    /**
     * 地图添加轨迹点
     */
    public static void addTrajectoryMulch(Context context, MapView map_view, GeoPoint geoPoint) {
        addCustomImgMulch(context, map_view, geoPoint, R.mipmap.map_marker_g_point);
    }

    /**
     * 地图添加多边形覆盖物
     */
    public static void addPolygonMulch(MapView map_view, ArrayList<GeoPoint> geoPoints) {
        ArrayList<GeoPoint> points = new ArrayList<>(geoPoints);

        PlaneOption option = new PlaneOption();
        //设置填充颜色
        option.setFillColor(Color.parseColor("#77049c53"));
        //设置笔画颜色
        option.setStrokeColor(Color.parseColor("#D9B35B"));
        //设置笔画宽度
        option.setStrokeWidth(20);
        //设置虚线
        option.setDottedLine(false);

        PolygonOverlay overlay = new PolygonOverlay();
        overlay.setOption(option);
        overlay.setPoints(points);
        map_view.addOverlay(overlay);
    }

    /**
     * 定位到林地区域中心
     */
    public static void locatePolygonMulchCenter(MapView map_view, List<GeoPoint> geoPoints) {
        ArrayList<Integer> latitude = new ArrayList<>();
        ArrayList<Integer> longitude = new ArrayList<>();

        for (int i = 0; i < geoPoints.size(); i++) {
            latitude.add(geoPoints.get(i).getLatitudeE6());
            longitude.add(geoPoints.get(i).getLongitudeE6());
        }

        int biggestLatitude = 0;
        //获取最大纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer > biggestLatitude) {
                biggestLatitude = integer;
            }
        }

        int minimumLatitude = latitude.get(0);
        //获取最小纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer < minimumLatitude) {
                minimumLatitude = integer;
            }
        }


        int biggestLongitude = 0;
        //获取最大经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer > biggestLongitude) {
                biggestLongitude = integer;
            }
        }


        int minimumLongitude = longitude.get(0);
        //获取最小经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer < minimumLongitude) {
                minimumLongitude = integer;
            }
        }

        //通过经纬度缩放级别
        map_view.getController().setZoom(getZoom(biggestLongitude, minimumLongitude, biggestLatitude, minimumLatitude));

        GeoPoint point = new GeoPoint((biggestLatitude + biggestLatitude) / 2, (biggestLongitude + minimumLongitude) / 2);
        //动画移动到当前位置
        map_view.getController().animateTo(point);
        LogUtils.v("gogogo get zoom = " + getZoom(biggestLongitude, minimumLongitude, biggestLatitude, minimumLatitude) + "");

    }


    /**
     * 绘制巡林轨迹
     */
    public static void drawTheRangerTrajectory(MapView map_view, List<RecorddetailsBean.Data.Coordinates> data) {

        //巡林轨迹中心定位
        rangerTrajectoryCenterPositioning(map_view, data);

        ArrayList<GeoPoint> points = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            points.add(new GeoPoint((int) (data.get(i).getLatitude() * 1E6), (int) (data.get(i).getLongitude() * 1E6)));
        }

        LineOption lineOption = new LineOption();
        lineOption.setStrokeWidth(9);
        lineOption.setStrokeColor(Color.parseColor("#0000ff"));
        PolylineOverlay lineOverlay = new PolylineOverlay();
        lineOverlay.setOption(lineOption);
        lineOverlay.setPoints(points);
        map_view.addOverlay(lineOverlay);

    }

    /**
     * 绘制巡林轨迹点
     */
    public static void drawTrajectoryByDot(Context context, MapView map_view, List<RecorddetailsBean.Data.Coordinates> data) {
        if (map_view == null || data == null || data.isEmpty()) {
            return;
        }

        //巡林轨迹中心定位
        rangerTrajectoryCenterPositioning(map_view, data);

        for (int i = 0; i < data.size(); i++) {
            GeoPoint point = new GeoPoint((int) (data.get(i).getLatitude() * 1E6), (int) (data.get(i).getLongitude() * 1E6));
            MulchUtil.addTrajectoryMulch(context, map_view, point);
        }
    }

    /**
     * 巡林轨迹中心定位
     */
    public static void rangerTrajectoryCenterPositioning(MapView map_view, List<RecorddetailsBean.Data.Coordinates> patrollingTasks) {
        ArrayList<Integer> latitude = new ArrayList<>();
        ArrayList<Integer> longitude = new ArrayList<>();

        for (int i = 0; i < patrollingTasks.size(); i++) {
            RecorddetailsBean.Data.Coordinates center = patrollingTasks.get(i);
            latitude.add((int) (center.getLatitude() * 1E6));
            longitude.add((int) (center.getLongitude() * 1E6));
        }

        if (latitude.isEmpty() || longitude.isEmpty()) {
            return;
        }

        int biggestLatitude = 0;
        //获取最大纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer > biggestLatitude) {
                biggestLatitude = integer;
            }
        }


        int minimumLatitude = latitude.get(0);
        //获取最小纬度
        for (int i = 0; i < latitude.size(); i++) {
            int integer = latitude.get(i);
            if (integer < minimumLatitude) {
                minimumLatitude = integer;
            }
        }


        int biggestLongitude = 0;
        //获取最大经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer > biggestLongitude) {
                biggestLongitude = integer;
            }
        }


        int minimumLongitude = longitude.get(0);
        //获取最小经度
        for (int i = 0; i < longitude.size(); i++) {
            int integer = longitude.get(i);
            if (integer < minimumLongitude) {
                minimumLongitude = integer;
            }
        }

        //通过经纬度缩放级别
        map_view.getController().setZoom(getZoom(biggestLongitude, minimumLongitude, biggestLatitude, minimumLatitude));

        GeoPoint point = new GeoPoint((biggestLatitude + biggestLatitude) / 2, (biggestLongitude + minimumLongitude) / 2);
        //动画移动到当前位置
        map_view.getController().animateTo(point);
    }

    /**
     * 绘制巡林轨迹 有返回值
     */
    public static PolylineOverlay drawTheRangerTrajectory2(MapView map_view, List<RecorddetailsBean.Data.Coordinates> data) {

        //巡林轨迹中心定位
        rangerTrajectoryCenterPositioning(map_view, data);

        ArrayList<GeoPoint> points = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            points.add(new GeoPoint((int) (data.get(i).getLatitude() * 1E6), (int) (data.get(i).getLongitude() * 1E6)));
        }

        LineOption lineOption = new LineOption();
        lineOption.setStrokeWidth(3);
        lineOption.setStrokeColor(Color.parseColor("#0000ff"));
        PolylineOverlay lineOverlay = new PolylineOverlay();
        lineOverlay.setOption(lineOption);
        lineOverlay.setPoints(points);
        map_view.addOverlay(lineOverlay);

        return lineOverlay;
    }

    /**
     * 获取两点间距离
     */
    public static double calDistance(GeoPoint point1, GeoPoint point2) {
        double distance = 0;
        try {
            Project project = new Project();
            distance = project.getDistanceMeters((float) (point1.getLongitudeE6() / 1E6),
                    (float) (point1.getLatitudeE6() / 1E6),
                    (float) (point2.getLongitudeE6() / 1E6),
                    (float) (point2.getLatitudeE6() / 1E6));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return distance;
    }
}
